package com.longrise.android.byjk.plugins.hra.pay;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.android.byjk.R;
import com.longrise.android.byjk.event.CourseDetailEvent;
import com.longrise.android.byjk.event.DiscountPayEvent;
import com.longrise.android.byjk.event.HraPaymentEvent;
import com.longrise.android.byjk.plugins.course.mycourse.MyCourseActivity;
import com.longrise.android.byjk.plugins.course.pay.BuyProtocolActivity;
import com.longrise.android.byjk.plugins.course.pay.OnPayItemListener;
import com.longrise.android.byjk.plugins.hra.HraMainActivity;
import com.longrise.android.byjk.plugins.hra.hratabsecond.MineAppointDetailActivity;
import com.longrise.android.byjk.utils.DialogUtil;
import com.longrise.android.byjk.widget.view.PayItemView;
import com.longrise.common.base.BaseActivity2;
import com.longrise.common.eventbean.PayEventBean;
import com.longrise.common.utils.DZZWTools;

import org.greenrobot.eventbus.EventBus;

/**
 * Created by callxkj on 2018/1/24.
 * Hra支付页面
 */

public class HraPaymentActivity extends BaseActivity2<HraPaymentPresenter> implements OnPayItemListener,
        View.OnClickListener, HraPaymentContract.View {

    private Button mBtBuy;
    private TextView mTvuserName;
    private TextView mTvzjhm;
    private PayItemView mPvwxzf;
    private PayItemView mPvalizf;
    private TextView mTvfwcs;
    private TextView mTvfwzj;

    private LinearLayout mLLprotocol;
    private CheckBox mCbprotocol;
    private TextView mTvprotocol;
    private String mOrderid;
    private String mPayname;
    private String mPaycardno;
    private String mNumber;
    private String mPrice;
    private CheckBox mCbdiscountCoupon;
    private RelativeLayout mRldiscountcoupon;
    private TextView mTvdiscountcoupon;


    /**
     * 开启当前的支付页面
     *
     * @param orderid   订单编号
     * @param payname   购买的用户姓名
     * @param paycardno 证件号码
     * @param number    hra服务次数
     * @param price     hra服务的总价
     */
    public static void toPushActivity(Activity activity, String orderid, String payname,
                                      String paycardno, String number, String price) {
        Intent intent = new Intent(activity, HraPaymentActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("orderid", orderid);
        bundle.putString("payname", payname);
        bundle.putString("paycardno", paycardno);
        bundle.putString("number", number);
        bundle.putString("price", price);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    private void getIntentData() {
        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
        if (extras != null) {
            mOrderid = extras.getString("orderid");
            mPayname = extras.getString("payname");
            mPaycardno = extras.getString("paycardno");
            mNumber = extras.getString("number");
            mPrice = extras.getString("price");
        }
    }

    @Override
    public int getContentViewId(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_hrapayment;
    }

    @Override
    public void initView() {
        setToolbarTitle("支付页面");
        mBtBuy = (Button) findViewById(R.id.hrapayment_buy_bt);
        mPvwxzf = (PayItemView) findViewById(R.id.hrapayment_wx);
        mPvalizf = (PayItemView) findViewById(R.id.hrapayment_zfb);
        mTvuserName = (TextView) findViewById(R.id.hrapayment_username_tv);
        mTvzjhm = (TextView) findViewById(R.id.hrapayment_zjhm_tv);
        mTvfwcs = (TextView) findViewById(R.id.hrapayment_kcsl_tv);
        mTvfwzj = (TextView) findViewById(R.id.hrapayment_kczj_tv);

        //hra优惠券
        mCbdiscountCoupon = (CheckBox) findViewById(R.id.hrapayment_hra_free_checkbox);
        mRldiscountcoupon = (RelativeLayout) findViewById(R.id.hrapayment_hra_free_ll);
        mTvdiscountcoupon = (TextView) findViewById(R.id.hrapayment_hra_free_tv);

        //注册协议
        mLLprotocol = (LinearLayout) findViewById(R.id.hrapayment_protocol_ll);
        mCbprotocol = (CheckBox) findViewById(R.id.hrapayment_checkbox);
        mTvprotocol = (TextView) findViewById(R.id.hrapayment_protocol_tv);

        mPvwxzf.setImage(R.drawable.by_ic_pay_we);
        mPvwxzf.setName(mContext.getString(R.string.wxzf));
        mPvalizf.setImage(R.drawable.by_ic_pay_ali);
        mPvalizf.setName(mContext.getString(R.string.zfbzf));
        mPvwxzf.setChecked(true);

        getIntentData();
        initEvent();
        initData();
    }

    @Override
    public void onToolbarBackClick() {
        finish();
    }

    private void initEvent() {
        mPvwxzf.setOnPayItemListener(this);
        mPvalizf.setOnPayItemListener(this);
        mBtBuy.setOnClickListener(this);
        mTvprotocol.setOnClickListener(this);
        mRldiscountcoupon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mCbdiscountCoupon.isChecked()) {
                    mCbdiscountCoupon.setChecked(false);
                } else {
                    mCbdiscountCoupon.setChecked(true);
                }
            }
        });

        mCbdiscountCoupon.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mPvwxzf.setChecked(false);
                    mPvalizf.setChecked(false);
                    mRldiscountcoupon.setBackgroundColor(Color.parseColor("#FAEDD4"));
                    mTvdiscountcoupon.setTextColor(Color.parseColor("#FBA140"));
                    mBtBuy.setText("支付  ¥0");
                } else {
                    if(!mPvalizf.isChecked()){
                        mPvwxzf.setChecked(true); //如果支付宝没有选择的话 默认选择微信
                    }
                    mRldiscountcoupon.setBackgroundColor(Color.parseColor("#F07878"));
                    mTvdiscountcoupon.setTextColor(Color.parseColor("#FFFFFF"));
                    mBtBuy.setText("支付  ¥" + mPrice);
                }
            }
        });

        mCbprotocol.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    mLLprotocol.setBackgroundColor(Color.parseColor("#FAEDD4"));
                    mTvprotocol.setTextColor(Color.parseColor("#FBA140"));
                    mBtBuy.setClickable(true);
                    mBtBuy.setBackgroundResource(R.drawable.by_bg_bt_square_selector);
                } else {
                    mLLprotocol.setBackgroundColor(Color.parseColor("#F07878"));
                    mTvprotocol.setTextColor(Color.parseColor("#FFFFFF"));
                    mBtBuy.setClickable(false);
                    mBtBuy.setBackgroundColor(Color.parseColor("#D8D8D8"));
                }
            }
        });
    }

    private void initData() {
        mPresenter.toGetDiscountInfo(mPaycardno);
        mCbprotocol.setChecked(true);
        mTvuserName.setText(mPayname);
        mTvzjhm.setText(mPaycardno);
        mTvfwcs.setText(mNumber);
        mTvfwzj.setText("¥" + mPrice);
    }

    /**
     * 微信与支付宝的选择
     */
    @Override
    public void onCheck(View view, Boolean checked) {
        switch (view.getId()) {
            case R.id.hrapayment_wx:
                mPvwxzf.setChecked(true);
                mPvalizf.setChecked(false);
                mCbdiscountCoupon.setChecked(false);
                break;
            case R.id.hrapayment_zfb:
                mPvalizf.setChecked(true);
                mPvwxzf.setChecked(false);
                mCbdiscountCoupon.setChecked(false);
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hrapayment_buy_bt: //点击购买
                toBuy();
                break;

            case R.id.hrapayment_protocol_tv: //保易健康购买服务协议
                startActivity(BuyProtocolActivity.class);
                break;

            case R.id.dialog_courses_evaluate_bt:  //微信支付宝 支付失败的确定
                DialogUtil.getInstance().dismiss();
                break;
        }
    }

    /**
     * 购买
     */
    private void toBuy() {
        if (mPvwxzf.isChecked()) {
            mPresenter.toWxpay(mOrderid);
        } else if (mPvalizf.isChecked()) {
            mPresenter.toAlipay(mOrderid);
        } else if (mCbdiscountCoupon.isChecked()) {
            mPresenter.toDiscountpay(mOrderid);
        }
    }

    @Override
    public void showWxPayErrorDialog() {
        View mDialogview = View.inflate(mContext, R.layout.dialog_courses_evaluate, null);
        TextView mTvtitle = (TextView) mDialogview.findViewById(R.id.dialog_courses_title);
        mTvtitle.setText("微信支付");
        ImageView mIv = (ImageView) mDialogview.findViewById(R.id.dialog_share_success_iv);
        mIv.setImageResource(R.drawable.icon_tips);
        TextView mTvcontent = (TextView) mDialogview.findViewById(R.id.dialog_courses_content);
        mTvcontent.setText("支付失败");
        TextView mTvbt = (TextView) mDialogview.findViewById(R.id.dialog_courses_evaluate_bt);
        mTvbt.setOnClickListener(this);
        Dialog dialog = DialogUtil.getInstance().creatAlertDialog(mContext, mDialogview, 266, 195);
        dialog.setCancelable(false);
    }

    @Override
    public void showAliPayErrorDialog() {
        View mDialogview = View.inflate(mContext, R.layout.dialog_courses_evaluate, null);
        TextView mTvtitle = (TextView) mDialogview.findViewById(R.id.dialog_courses_title);
        mTvtitle.setText("支付宝支付");
        ImageView mIv = (ImageView) mDialogview.findViewById(R.id.dialog_share_success_iv);
        mIv.setImageResource(R.drawable.icon_tips);
        TextView mTvcontent = (TextView) mDialogview.findViewById(R.id.dialog_courses_content);
        mTvcontent.setText("支付失败");
        TextView mTvbt = (TextView) mDialogview.findViewById(R.id.dialog_courses_evaluate_bt);
        mTvbt.setOnClickListener(this);
        Dialog dialog = DialogUtil.getInstance().creatAlertDialog(mContext, mDialogview, 266, 195);
        dialog.setCancelable(false);
    }

    @Override
    public void toShowDiscountCoupon(boolean isHas) {
        if (isHas) { //该用户有优惠券
            mRldiscountcoupon.setVisibility(View.VISIBLE);
            mCbdiscountCoupon.setChecked(true);
            mPvalizf.setChecked(false);
            mPvwxzf.setChecked(false);
            mBtBuy.setText("支付  ¥0");
        } else {
            mRldiscountcoupon.setVisibility(View.GONE);
            mPvalizf.setChecked(false);
            mPvwxzf.setChecked(true);
            mBtBuy.setText("支付  ¥" + mPrice);
        }
    }

    /**
     *
     * @param isDiscountpay  是否是余额支付成功
     */
    @Override
    public void showPaySuccess(boolean isDiscountpay) {
        //通知关闭前面两个页面
        HraPaymentEvent paymentEvent = new HraPaymentEvent();
        paymentEvent.setClose(true);
        EventBus.getDefault().post(paymentEvent);

        //如果是优惠券购买成功就通知首页 刷新数据
        if(isDiscountpay){
            DiscountPayEvent event = new DiscountPayEvent();
            event.setDiscountpay(true);
            EventBus.getDefault().post(event);
        }

        View dialogview = View.inflate(this, R.layout.by_dialog_pay_success, null);
        LinearLayout ll_confirm = (LinearLayout) dialogview.findViewById(R.id.dialog_pay_success_confirm_ll);
        final Dialog dialog = DialogUtil.getInstance().creatAlertDialog(this, dialogview, 270, 144);
        dialog.setCancelable(false);
        ll_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Bundle bundle = new Bundle();
                bundle.putString(MineAppointDetailActivity.MINEORDERID,mOrderid);
                bundle.putBoolean(MineAppointDetailActivity.SHOWFORKBACK,true);
                startActivity(MineAppointDetailActivity.class,bundle);

                finish();

            }
        });
    }

    @Override
    public void showNotBackWaitDialog() {
        DZZWTools.showNotBackWaitDialog(this);
    }

    @Override
    public void dismissNotBackDialog() {
        DZZWTools.dismissNotBackDialog();
    }

    @Override
    public void showPayError() {
        //通知关闭前面两个页面
        HraPaymentEvent paymentEvent = new HraPaymentEvent();
        paymentEvent.setClose(true);
        EventBus.getDefault().post(paymentEvent);

        View dialogview = View.inflate(this, R.layout.by_dialog_pay_error, null);
        LinearLayout ll_confirm = (LinearLayout) dialogview.findViewById(R.id.dialog_pay_error_confirm_ll);
        final Dialog dialog = DialogUtil.getInstance().creatAlertDialog(this, dialogview, 270, 144);
        dialog.setCancelable(false);
        ll_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
                Bundle bundle = new Bundle();
                bundle.putString(MineAppointDetailActivity.MINEORDERID,mOrderid);
                bundle.putBoolean(MineAppointDetailActivity.SHOWFORKBACK,true);
                startActivity(MineAppointDetailActivity.class,bundle);

                finish();

            }
        });
    }
}
